//
//  PrierLIstCtrt.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/20.
//  Copyright © 2016年 game. All rights reserved.
//

#import "PrierLIstCtrt.h"
#import "NewsCell.h"
#import "UIImageView+WebCache.h"
#import "NewestAticleCtr.h"
#import "ShoolDataModel.h"
#import "NewstModel.h"
#import "NEWAticListModel.h"
#import "NewRecomModel.h"
#import "CGFdbmBase.h"
#import "Recomment.h"
#import "PageInfoModel.h"
#import "VideoDescrib.h"
#import "PersonNavView.h"
#import "MJRefresh.h"
@interface PrierLIstCtrt ()<UITableViewDataSource,UITableViewDelegate>
{
        CGFloat _cellHeight;
    ShoolDataModel *_model;
    NewstModel *_newstModel;
    NEWAticListModel *_AticModel3;
    NewRecomModel *_recomModel;
    PageInfoModel *_infoModel;
    Recomment *_recommentView;
}
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,strong) NSMutableArray *recomAry;
@end

@implementation PrierLIstCtrt

-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
    }
    return _listAry;
}

-(NSMutableArray *)recomAry{
    if (!_recomAry) {
        _recomAry  = [NSMutableArray array];
    }
    return _recomAry;
}

-(void)viewWillAppear:(BOOL)animated{
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}
-(void)NavView{
    PersonNavView *naview = [[PersonNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    naview.title.text = self.listName;
    
    __weak typeof(self) WeakSelf = self;
    naview.BackToFatherView = ^{
        [WeakSelf.navigationController popViewControllerAnimated:YES];
    };
    [self.view addSubview:naview];
}

-(void)GetAFNtWhithPage:(NSInteger )page{
    
    NSString *url;
    
    if (self.isUrl==NO ) {
        if (page==1) {
             url = [NSString stringWithFormat:@"http://school.diaoyur.cn/article/list-by-type-id?typeid=%ld",(long)self.ID];
        }else{
            url = [NSString stringWithFormat:@"http://school.diaoyur.cn/article/list-by-type-id?typeid=%ld&page=%ld",(long)self.ID,(long)page];
        }
    }else{
    
        if (page==1) {
            url = self.AtcUrl;
        }else{
            
            url = [NSString stringWithFormat:@"%@&page=%ld",self.AtcUrl,(long)page];
        }
    }

    [[Manager sharedManager]RequestWhithURL:url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        _model = [[ShoolDataModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        [self.listAry addObjectsFromArray:_model.article_list];
        
        if (_listAry.count>10) {
            
            _tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        }

        _infoModel = _model.page_info;
     
            if (_recomAry.count<=0 && _model.recommend.count != 0) {
                [self.recomAry addObjectsFromArray:_model.recommend];
                 [self HeadOfTableview];
            }
       
        [_tableview reloadData];
        [self.tableview.header endRefreshing];
        [self.tableview.footer endRefreshing];
    }];
}

#pragma mark--下拉刷新
- (void)loadNewData {
    [_recomAry removeAllObjects];
    [_listAry removeAllObjects];
    [self GetAFNtWhithPage:1];
}
#pragma mark--上拉加载
- (void)loadMoreData {
    
    if (![_infoModel.nextPage isEqualToString:_infoModel.pageCount]) {
        [self GetAFNtWhithPage:_infoModel.nextPage.integerValue];
    }else{
        [_tableview.footer noticeNoMoreData];
    }
}

- (void)viewDidLoad {

    [super viewDidLoad];
    [self NavView];
 
     [self CrectTableview];
    [self GetAFNtWhithPage:1];
}

-(void)CrectTableview{
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableview.dataSource = self;
    _tableview.delegate = self;
     _tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableview registerClass:[NewsCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
    _tableview.tableFooterView = [UIView new];
    
    _tableview.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
}

#pragma mark--推荐列表
-(void)HeadOfTableview{
     _recommentView = [[Recomment alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.23)];
    _recommentView.titleAry = [self.recomAry copy];
    __weak typeof(self) weakself = self;
    _recommentView.Click = ^(NSNumber *skin,NSString *AticID){
        
        NewestAticleCtr *aticle = [NewestAticleCtr new];
        aticle.AticleID =AticID;
        [weakself.navigationController pushViewController:aticle animated:YES];
    };
    _tableview.tableHeaderView = _recommentView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    return self.listAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return _cellHeight;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model =self.listAry[indexPath.row];
    _cellHeight = cell.cellHight;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    _AticModel3 = self.listAry[indexPath.row];
    
        NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NewestAticleCtr *aticle = [NewestAticleCtr new];
        aticle.AticleID = cell.AticleID;
        [self.navigationController pushViewController:aticle animated:YES];
}
-(void)didReceiveMemoryWarning{
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    
    if ([self.view window] == nil) {
        
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        
    }
}

@end